package com.chat.controller;

import com.chat.entry.dto.user.*;
import com.chat.result.Result;
import com.chat.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "用户接口管理")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @ApiOperation(value = "生成图形验证码", notes = "生成图形验证码成功")
    @GetMapping("/generateVerificationCode")
    public Result generateVerificationCode() {
        return userService.generateVerificationCode();
    }

    @ApiOperation(value = "发送手机验证码", notes = "向指定手机号发送验证码成功")
    @GetMapping("/sendVerificationCode")
    public Result sendVerificationCode(
            @ApiParam(value = "手机号", required = true) @RequestParam("phoneNumber") String phoneNumber,
            @ApiParam(value = "图形验证码", required = true) @RequestParam("imageCode") String imageCode
    ) {
        return userService.sendVerificationCode(phoneNumber, imageCode);
    }

    @ApiOperation(value = "注册账号", notes = "注册新用户账号成功")
    @PostMapping("/register")
    public Result register(
            @ApiParam(value = "用户注册信息", required = true) @RequestBody UserRegisterDTO userRegisterDTO,
            @ApiParam(value = "手机验证码", required = true) @RequestParam String phoneCode
    ) {
        return userService.register(userRegisterDTO, phoneCode);
    }

    @ApiOperation(value = "用户号登录账号", notes = "使用用户名登录")
    @PostMapping("/username/login")
    public Result login(
            @ApiParam(value = "用户登录信息", required = true) @RequestBody UserRegisterDTO userRegisterDTO
    ) {
        return userService.login(userRegisterDTO);
    }

    @ApiOperation(value = "手机号登录账号", notes = "使用手机号登录")
    @PostMapping("/phone/login")
    public Result phoneLogin(
            @ApiParam(value = "手机号和手机号验证码", required = true) @RequestBody PhoneLoginDTO phoneLoginDTO
    ) {
        return userService.phoneLogin(phoneLoginDTO);
    }

    @ApiOperation(value = "修改用户号", notes = "更新用户唯一用户名成功")
    @PutMapping("/update/{uid}/username")
    public Result updateUsername(
            @ApiParam(value = "用户ID", required = true) @PathVariable("uid") Integer uid,
            @ApiParam(value = "新用户名", required = true) @RequestBody UpdateUsernameDTO updateUsernameDTO
    ) {
        return userService.updateUsername(uid, updateUsernameDTO);
    }

    @ApiOperation(value = "修改简介", notes = "更新用户个人简介成功")
    @PutMapping("/update/{uid}/intro")
    public Result updateIntro(
            @ApiParam(value = "用户ID", required = true) @PathVariable("uid") Integer uid,
            @ApiParam(value = "新个人简介", required = true) @RequestBody UpdateIntroDTO updateIntroDTO
    ) {
        return userService.updateIntro(uid, updateIntroDTO);
    }

    @ApiOperation(value = "修改密码", notes = "更新用户登录密码成功")
    @PutMapping("/update/{uid}/password")
    public Result updatePassword(
            @ApiParam(value = "用户ID", required = true) @PathVariable("uid") Integer uid,
            @ApiParam(value = "新密码", required = true) @RequestBody UpdatePasswordDTO updatePasswordDTO
    ) {
        return userService.updatePassword(uid, updatePasswordDTO);
    }

    @ApiOperation(value = "修改用户信息", notes = "更新完整用户信息成功")
    @PutMapping("/update/{uid}/complete")
    public Result updateComplete(
            @ApiParam(value = "用户ID", required = true) @PathVariable("uid") Integer uid,
            @ApiParam(value = "完整用户信息", required = true) @RequestBody UpdateUserCompleteDTO updateUserCompleteDTO
    ) {
        return userService.updateComplete(uid, updateUserCompleteDTO);
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户的基本信息")
    @GetMapping("/{userId}")
    public Result getUser(
            @ApiParam(value = "用户id", required = true) @PathVariable Integer userId
    ) {
        return userService.getUser(userId);
    }

    @ApiOperation(value = "忘记密码", notes = "通过手机号修改密码")
    @GetMapping("/forget/password")
    public Result forgetPassword(
            @ApiParam(value = "用户手机号", required = true) @RequestParam String phoneNumber,
            @ApiParam(value = "新密码",required = true)@RequestParam String password
    ) {
        return userService.forgetPassword(phoneNumber,password);
    }

    @ApiOperation(value = "发送手机验证码", notes = "不验证图像验证码发送手机验证码")
    @GetMapping("/sendPhoneCode")
    public Result sendPhoneCode(
            @ApiParam(value = "用户手机号", required = true) @RequestParam String phoneNumber
    ) {
        return userService.sendPhoneCode(phoneNumber);
    }
}
